﻿@model NCP.Model.GroupPurchase
@{
    ViewBag.Title = "集采药品维护";
}
<div id="LAY-gd-body" class="layui-card" style="margin-bottom:0px;">
    <div class="layui-card-header" style="padding-bottom:10px;padding-top:10px;">
        <span style="margin-left:10px;margin-right:100px;font-size:large;font-weight:600;">@Model.Name [@Model.Code]</span>
        @if (Model.Enabled != "1")
        {
            <button id="doSave" class="layui-btn submit" type="button">保存</button>
        }
        <button id="onCancel" class="layui-btn layui-btn-danger" type="button">关闭</button>
        <button id="doDownload" class="layui-btn layui-btn-normal" type="button" style="float:right;">下载模板</button>
        <button id="doImport" class="layui-btn layui-btn-normal" type="button" style="float:right;">导入</button>
        <button id="doExportDrug" class="layui-btn layui-btn-normal" type="button" style="float:right;">导出</button>
    </div>
    <div class="layui-row">
        <div class="layui-col-md5">
            <div class="layui-transfer-search" style="border-width: 1px;">
                <i class="layui-icon layui-icon-search"></i>
                <input id="leftsearch" type="text" class="layui-input" placeholder="关键词搜索" />
            </div>
            <table id="left_tab" lay-filter="left"></table>
        </div>
        <div class="layui-col-md1 btn-height">
            <div style="margin-bottom: 10px;">
                <button class="layui-btn  layui-btn-disabled left-btn">
                    <i class="layui-icon">&#xe602;</i>
                </button>
            </div>
            <div>
                <button class="layui-btn layui-btn-disabled right-btn">
                    <i class="layui-icon">&#xe603;</i>
                </button>
            </div>
        </div>
        <div class="layui-col-md6">
            <div class="layui-transfer-search" style="border-width: 1px;">
                <i class="layui-icon layui-icon-search"></i>
                <input id="rightsearch" type="text" class="layui-input" placeholder="关键词搜索" />
            </div>
            <table id="right_tab" lay-filter="right"></table>
        </div>
    </div>
</div>
<script type="text/javascript" src="~/Scripts/jsonDB.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'admin', 'upload'], function () {
        var table = layui.table, $ = layui.$, admin = layui.admin, upload = layui.upload;
        //计算按钮的高度
        $('.btn-height').css('padding-top', (parent.document.body.clientHeight / 2 - 150)).css('text-align', 'center');
        //左边表格
        function initDataList() {
            admin.req({
                url: '/GroupPurchase/GroupDrugAllDrugList' //实际使用请改成服务端真实接口
                , method: 'post'
                , data: "gpCode=@Request["GpCode"]"
                , done: function (res) {
                    jsonDB(res.data, 'leftdata').init('leftDB');
                    table.render({
                        elem: '#left_tab'
                        , data: res.data
                        , cols: [[
                            { checkbox: true, fixed: true }
                            , { field: 'DrugName', title: '【未选药品】', width: 200 }
                            , { field: 'Specs', title: '规格', width: 160 }
                            , { field: 'UnitPrice', title: '单价', width: 100 }
                            , { field: 'ProducerName', title: '生产厂家', width: 160 }
                            , { field: 'DrugCode', title: '药品编码', width: 130 }
                        ]]
                        , id: 'left_table_id'
                        , height: 'full-120'
                        , page: true
                        , limit: 100
                        , limits: [10, 50, 100, 200, 500, 1000, 2000]
                    });
                }
            });
            admin.req({
                url: '/GroupPurchase/GroupDrugChoseDrugList' //实际使用请改成服务端真实接口
                , method: 'post'
                , data: "gpCode=@Request["GpCode"]"
                , done: function (res) {
                    jsonDB(res.data, 'rightdata').init('rightDB');
                    table.render({
                        elem: '#right_tab'
                        , data: res.data
                        , cols: [[
                            { checkbox: true, fixed: true }
                            , { field: 'DrugName', title: '【已选药品】', width: 200 }
                            , { field: 'Specs', title: '规格', width: 150 }
                            , { field: 'TaskQTY', title: '任务量', width: 100, edit: 'text' }
                            , { field: 'TaskRatio', title: '规定使用占比(0-1)', width: 160, edit: 'text' }
                            , { field: 'UnitPrice', title: '单价', width: 90 }
                            , { field: 'ProducerName', title: '生产厂家', width: 160 }
                            , { field: 'DrugCode', title: '药品编码', width: 130 }

                        ]]
                        , id: 'right_table_id'
                        , height: 'full-120'
                        , page: true
                        , limit: 100
                        , limits: [10, 50, 100, 200, 500, 1000, 2000]
                    });
                }
            });
        }
        initDataList();
        upload.render({ //允许上传的文件后缀
            elem: '#doImport'
            , url: '/GroupPurchase/ImportDrug?gpCode=@Request["GpCode"]' //此处配置你自己的上传接口即可
            , accept: 'file' //普通文件
            , exts: 'xls|xlsx' //只允许上传表格文件
            , done: function (res) {
                if (res == "0") {
                    layer.alert("导入成功！");
                    initDataList();
                } else {
                    layer.alert(res.msg);
                }
            }
            , error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status + ' ' + textStatus + ' (' + errorThrown + ')');
            }
        });
        //判断输入的值是否为数字，不是数字提示并赋值回原本的值
        table.on('edit(right)', function (obj) {
            obj.data.Edited = '1';
            //获取编辑前的值
            //判断数据类型
            if (obj.field == 'TaskQTY' || obj.field == 'TaskRatio') {
                var val = obj.value;
                if (!isNumber(obj.value)) {
                    layer.msg('请输入数字');
                    // 重点 赋值
                    val = $(this).prev().text();
                }
                var val = parseFloat(val);
                $(this).val(val);
                rightDB.query('update rightdata set ' + obj.field + '="' + val + '" where (DrugCode="' + obj.data.DrugCode + '") limit 1');
            }
            console.log(obj.value); //得到修改后的值
            console.log(obj.field); //当前编辑的字段名
            console.log(obj.data); //所在行的所有相关数据
        });
        //判断是非数字
        function isNumber(val) {
            var regPos = /^\d+(\.\d+)?$/; //非负浮点数
            var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
            if (regPos.test(val) || regNeg.test(val)) {
                return true;
            } else {
                return false;
            }
        }

        $("#leftsearch").on('input',function () {
            var val = $(this).val().toUpperCase();
            var result = leftDB.findAll("leftdata");
            var ldata = result.filter(e => { return e.DrugCode.indexOf(val) >= 0 || e.DrugName.indexOf(val) >= 0 || e.SpellCode.indexOf(val) >= 0 });
            reloadleftTable(ldata);
        });
        $("#rightsearch").on('input', function () {
            var val = $(this).val().toUpperCase();
            var result = leftDB.findAll("rightdata");
            var rdata = result.filter(e => { return e.DrugCode.indexOf(val) >= 0 || e.DrugName.indexOf(val) >= 0 || e.SpellCode.indexOf(val) >= 0 });
            reloadrightTable(rdata);
        });
        //公共方法
        function getTableData(id) {
            var checkStatus = table.checkStatus(id)
                , data = checkStatus.data;
            return data;
        }
        function btnIf(data, btn) {
            if (data && data.length) {
                $(btn).removeClass('layui-btn-disabled')
            } else {
                $(btn).addClass('layui-btn-disabled')
            }
        }

        function reloadleftTable(ldata) {
            table.reload('left_table_id', {
                data: ldata,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }

        function reloadrightTable(rdata) {
            table.reload('right_table_id', {
                data: rdata,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }
        //监听左表格选中
        table.on('checkbox(left)', function (obj) {
            btnIf(getTableData('left_table_id'), '.left-btn');
        });
        //监听右表格选中
        table.on('checkbox(right)', function (obj) {
            btnIf(getTableData('right_table_id'), '.right-btn');
        });
        //左按钮点击移动数据
        $('.left-btn').on('click', function () {
            if (!$(this).hasClass('layui-btn-disabled')) {
                $('.left-btn,.right-btn').addClass('layui-btn-disabled')
                var data = getTableData('left_table_id');
                for (n in data) {
                    leftDB.query('delete from leftdata where (DrugCode="' + data[n].DrugCode + '") limit 1');
                    rightDB.table('rightdata').addFirst({
                        DrugCode: data[n].DrugCode,
                        DrugName: data[n].DrugName,
                        Specs: data[n].Specs,
                        UnitPrice: data[n].UnitPrice,
                        ProducerName: data[n].ProducerName,
                        SpellCode: data[n].SpellCode,
                        TaskQTY: data[n].TaskQTY,
                        TaskRatio: data[n].TaskRatio,
                        Edited : '0'
                    });
                }
                reloadleftTable(leftDB.findAll("leftdata"));
                reloadrightTable(rightDB.findAll("rightdata"));
            }
        });
        //右按钮点击移动数据
        $('.right-btn').on('click', function () {
            if (!$(this).hasClass('layui-btn-disabled')) {
                $('.left-btn,.right-btn').addClass('layui-btn-disabled')
                var data = getTableData('right_table_id');
                for (n in data) {
                    var rdata = rightDB.findAll("rightdata").filter(e => { return e.DrugCode == data[n].DrugCode });
                    for (t in rdata) {
                        rightDB.query('delete from rightdata where (DrugCode="' + rdata[t].DrugCode + '") limit 1');
                    }
                    leftDB.table('leftdata').addFirst({
                        DrugCode: data[n].DrugCode,
                        DrugName: data[n].DrugName,
                        Specs: data[n].Specs,
                        UnitPrice: data[n].UnitPrice,
                        ProducerName: data[n].ProducerName,
                        SpellCode: data[n].SpellCode,
                        TaskQTY: data[n].TaskQTY,
                        TaskRatio: data[n].TaskRatio,
                    });
                }
                reloadrightTable(rightDB.findAll("rightdata"));
                reloadleftTable(leftDB.findAll("leftdata"));
            }
        });

        $('#doSave').on('click', function () {
            var data = rightDB.findAll("rightdata");
            $.ajax({
                url: '/GroupPurchase/SaveGroupDrug',
                type: 'post',
                dataType: 'json',
                data: { items: data , gpCode: '@Request["GpCode"]'},
                success: function (res) {
                    layer.alert(res.msg);
                }
            });
        });
        $('#onCancel').on('click', function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
        $('#doDownload').on('click', function () {
            var form = document.createElement('form');
            form.action = '/GroupPurchase/ImportTemplet';
            form.method = 'post';
            $(document.body).append(form);
            form.submit();
        });
        $('#doExportDrug').on('click', function () {
            var form = document.createElement('form');
            form.action = '/GroupPurchase/ExportDrug?gpCode=@Request["GpCode"]&name=@Model.Name'+'集采药品维护';
            form.method = 'post';
            $(document.body).append(form);
            form.submit();
        });
    });
</script>
